package com.cardbaobao.cardbabyclient.ui.activity.web.share;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.dialog.LoadingDialog;
import com.cardbaobao.cardbabyclient.ui.pop.WebSharePop;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by Jiangzx on 2017/9/18 18:04.
 */

public class WebShare {
    private Activity mActivity;

    public WebShare(Activity activity) {
        this.mActivity = activity;
    }

    WebSharePop pop;
    ProgressDialog dialog;

    public void showDialog(){
        if(dialog==null){
            dialog=ProgressDialog.show(mActivity,
                    null,
                    mActivity.getString(R.string.string_wait_a_moment));
        }
        try{
            if(!dialog.isShowing()){
                dialog.show();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void dismissDialog(){
        try{
            if(dialog!=null&&dialog.isShowing()){
                dialog.dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void share(String url, String title, String content, String imgUrl) {
        if (pop == null) {
            pop = new WebSharePop(mActivity);
        }
        final LoadingDialog dialog = new LoadingDialog(mActivity);
        dialog.setShowText("初始化...");
        UMImage umImage = new UMImage(mActivity, imgUrl);
        final UMWeb webShare = new UMWeb(url);
        webShare.setThumb(umImage);
        webShare.setTitle(title);
        webShare.setDescription(content);
        final ShareAction shareAction = new ShareAction(mActivity).
                withMedia(webShare).
                setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        dialog.show();
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        dialog.dismiss();
                        ToastUtil.showToast("分享成功");
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        dialog.dismiss();
                        ToastUtil.showToast("分享失败");
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        dialog.dismiss();
                        ToastUtil.showToast("分享取消");
                    }
                });
        pop.setPopShareListener(new WebSharePop.PopShareListener() {
            @Override
            public void onPlatClicked(int position) {
                switch (position) {
                    case WebSharePop.PopShareListener.SHARE_WX:
                        //微信
                        shareAction.setPlatform(SHARE_MEDIA.WEIXIN).share();
                        break;
                    case WebSharePop.PopShareListener.SHARE_WX_CIRCLE:
                        //微信朋友圈
                        shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).share();
                        break;
                    case WebSharePop.PopShareListener.SHARE_QQ:
                        shareAction.setPlatform(SHARE_MEDIA.QQ);
                        break;
                }
            }
        });
        if (!pop.isShowing()) {
            View focus = mActivity.getCurrentFocus();
            if (focus != null) {
                pop.showAtLocation(focus, Gravity.NO_GRAVITY, 0, 0);
            }
        }
    }

    /**
     *
     * @param tage 分享到哪个平台
     * @param tit 分享标题
     * @param des 分享内容
     * @param pic 分享图片地址
     * @param url 分享url
     */
    public void share(String tage,String tit,String des,String pic,String url ){
        try{
            SHARE_MEDIA share_media;
            switch (tage){
                case "微信":
                    share_media=SHARE_MEDIA.WEIXIN;
                    break;
                case "朋友圈":
                    share_media=SHARE_MEDIA.WEIXIN_CIRCLE;
                    break;
                case "微博":
                    share_media=SHARE_MEDIA.SINA;
                    break;
                case "QQ":
                    share_media=SHARE_MEDIA.QQ;
                    break;
                default:
                    share_media=SHARE_MEDIA.WEIXIN;
                    break;
            }
            UMWeb web=new UMWeb(url);
            web.setTitle(tit);
            web.setDescription(des);
            web.setThumb(new UMImage(mActivity,pic));
            ShareAction shareAction=new ShareAction(mActivity).
                    setPlatform(share_media).
                    setCallback(umShareListener).
                    withMedia(web);
            shareAction.share();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private UMShareListener umShareListener=new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            showDialog();
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            dismissDialog();
            ToastUtil.showToast("分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            dismissDialog();
            ToastUtil.showToast("分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            dismissDialog();
            ToastUtil.showToast("分享取消");
        }
    };

    /**
     * 注意：在activity onActivityResult回调中手动调用此方法
     * @param requestCode requestCode
     * @param resultCode resultCode
     * @param data data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        dismissDialog();
        UMShareAPI.get(mActivity).onActivityResult(requestCode,resultCode,data);
    }
}
